package com.woniuxy.controller;


import cn.hutool.core.io.FileUtil;
import com.woniuxy.common.Result;
import com.woniuxy.controller.form.CommentSearchForm;
import com.woniuxy.controller.form.StoreReplayForm;
import com.woniuxy.service.CommentInfoService;
import com.woniuxy.service.dto.CommentCountDto;
import com.woniuxy.service.dto.WoniuPageDto;
import com.woniuxy.util.GetTypeByHead;
import com.woniuxy.util.OSSUtil;
import com.woniuxy.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 * 评价信息表 前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/commentInfo")
@Api(tags = "评论相关接口")
public class CommentInfoController {

    @Autowired
    private CommentInfoService service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/replay")
    @ApiOperation("商家回复评论")
    public Result replay(@RequestBody StoreReplayForm form, @RequestHeader String accessToken){
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        service.replay(form,accountId);
        return Result.success(200,"商家回复成功",null);
    }

    @PostMapping("/del")
    @ApiOperation("商家删除回复")
    public Result replay(Long id, @RequestHeader String accessToken){
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        service.delReplay(id,accountId);
        return Result.success(200,"商家回复删除成功",null);
    }

    @PostMapping("/search")
    @ApiOperation("展示该商家所有评论")
    public Result search(@RequestBody CommentSearchForm form, @RequestHeader String accessToken){
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        WoniuPageDto dto=service.searchForStore(form,accountId);
        return Result.success(200,"展示所有商家评论成功",dto);
    }



    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if ( !(fileType.equals("jpg")|| fileType.equals("png")|| fileType.equals("tif") ||fileType.equals("jpeg"))){
            throw new Exception("请上传图片类型的文件");
        }
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传图片成功",url);
        //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/606b7e7c-addf-4e65-8b2f-7e4bea2b4157.jpeg
    }

    @GetMapping("/count")
    @ApiOperation("统计")
    public Result countComment(@RequestHeader String accessToken) {
        Integer uid = redisUtil.getUserIdByToken(accessToken);
        CommentCountDto dto = service.countComment(uid);
        return Result.success(200, "ok", dto);
    }
}

